class CameraController {
    constructor(camera) {
        this.camera = camera;

        this.mouse0State = 'up';
        this.mouse1State = 'up';
        this.mouse2State = 'up';

        this.x = -1;
        this.y = -1;

        this.minDistance = 17;
        this.maxDistance = 20037508.3427892;

    }

    update(){
        let that = this;

        window.addEventListener('mousedown',function (e) {
            // console.log(e.button+' down '+e.clientX+','+e.clientY);
            let dom =  window.document.getElementsByTagName('canvas')[0];
            if(!dom){
                return;
            }
            let rect = dom.getBoundingClientRect();
            if(e.clientX < rect.left || e.clientX > rect.right || e.clientY < rect.top || e.clientY > rect.bottom){
                return;
            }
            if(e.button == 0){
                that.mouse0State = 'down';
            }else if(e.button == 1){
                that.mouse1State = 'down';
            }else if(e.button == 2){
                that.mouse2State = 'down';
            }
        });

        window.addEventListener('mouseup',function (e) {
            // console.log(e.button+' up '+e.clientX+','+e.clientY);
            if(e.button == 0){
                that.mouse0State = 'up';
            }else if(e.button == 1){
                that.mouse1State = 'up';
            }else if(e.button == 2){
                that.mouse2State = 'up';
            }
        });

        window.addEventListener('mousemove',function (e) {
            // console.log(e.button+' move '+e.clientX+','+e.clientY);

            let camera = that.camera;

            if(that.mouse0State == 'down'){
                let deltaX = e.clientX - that.x;
                let deltaY = e.clientY - that.y;

                let axisY = new THREE.Vector3(0,0,1);
                let axisX = new THREE.Vector3(1,0,0);

                let oldPosition = camera.position;

                let disPerPix = oldPosition.y*2/window.innerHeight;
                let newPosition = new THREE.Vector3().addVectors(oldPosition, axisY.clone().multiplyScalar(-deltaY*disPerPix));
                newPosition = new THREE.Vector3().addVectors(newPosition, axisX.clone().multiplyScalar(-deltaX*disPerPix));

                let newTarget = new THREE.Vector3(newPosition.x, 0, newPosition.z);

                camera.position.copy(newPosition);
                camera.lookAt(newTarget);

            }

            that.x = e.clientX;
            that.y = e.clientY;
        });

        window.addEventListener('mousewheel',function (e) {
            // console.log(e.button+' wheel '+e.wheelDelta);

            let camera = that.camera;

            let delta = e.wheelDelta;

            if(delta == 0 || typeof(delta) != 'number'){
                return;
            }
            delta = Math.abs(delta)/delta;

            let direction = new THREE.Vector3(0,-1,0);

            let oldPosition = camera.position;
            let dis = oldPosition.y;
            let newPosition = new THREE.Vector3().addVectors(oldPosition, direction.clone().multiplyScalar(dis/10*delta));

            if(newPosition.y<that.minDistance || newPosition.y>that.maxDistance){
                return;
            }
            camera.position.copy(newPosition);

        });
    }

}